# 定义颜色代码
GREEN = '\033[32m'
RESET = '\033[0m'

def hex_converter():
    try:
        hex_num = input("0x"+"请输入一个十六进制数:")

        decimal_num = int(hex_num, 16)
        binary_num = bin(decimal_num)[2:]

        print(f"\n转换结果:")
        print(f"十六进制：{GREEN}{hex_num}{RESET}")
        print(f"十进制：{GREEN}{decimal_num}{RESET}")
        print(f"二进制：{GREEN}{binary_num}{RESET}")
        
    except ValueError:
        print("输入错误！请确保输入的是有效的十六进制数。")
    except Exception as e:
        print(f"{str(e)}")

def binary_to_hex():
    try:
        binary_num = input("请输入一个二进制数（例如:1010):")
        
        decimal_num = int(binary_num, 2)
        hex_num = hex(decimal_num)
        
        print(f"\n转换结果:")
        print(f"二进制：{GREEN}{binary_num}{RESET}")
        print(f"十进制：{GREEN}{decimal_num}{RESET}")
        print(f"十六进制：{GREEN}{hex_num}{RESET}")
        
    except ValueError:
        print("输入错误！请确保输入的是有效的二进制数（只包含0和1）。")
    except Exception as e:
        print(f"发生错误：{str(e)}")

def decimal_converter():
    try:
        decimal_num = int(input("请输入一个十进制数:"))
        
        binary_num = bin(decimal_num)[2:]
        hex_num = hex(decimal_num)
        
        print(f"\n转换结果:")
        print(f"十进制：{GREEN}{decimal_num}{RESET}")
        print(f"二进制：{GREEN}{binary_num}{RESET}")
        print(f"十六进制：{GREEN}{hex_num}{RESET}")
        
    except ValueError:
        print("输入错误！请确保输入的是有效的十进制数。")
    except Exception as e:
        print(f"发生错误：{str(e)}")

def main():
    while True:
        print("\n请选择转换方式:")
        print("1. 十六进制转换为十进制和二进制")
        print("2. 二进制转换为十六进制和十进制")
        print("3. 十进制转换为二进制和十六进制")
        print("4. 退出程序")
        
        choice = input("请输入选项(1-4):")
        
        if choice == '1':
            hex_converter()
        elif choice == '2':
            binary_to_hex()
        elif choice == '3':
            decimal_converter()
        elif choice == '4':
            print("程序已退出！")
            break
        else:
            print("无效的选项，请重新选择！")

if __name__ == "__main__":
    main()
